package com.totcy.baselibrary.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.totcy.baselibrary.R;

import java.math.BigDecimal;

public class ProgressCircleView extends View {

    private int strokeWidthPc = 10; //进度条的宽度
    private float percent = 0f;  //进度值 0 ~ 1
    private int maxData = 100;
    private int currentData = 0;
    private Paint mCirclePaint;//进度条画笔
    private Paint mTextPaint;//文字画笔
    private int paddingPc = 20;

    private int defaultColor = Color.parseColor("#dedede");
    private int progressColor = Color.parseColor("#fd635e");
    private int textColor = Color.parseColor("#fd635e");
    private float textSize = 20f;
    private RectF rectF;
    private int cx, cy, radius;

    public ProgressCircleView(Context context) {
        this(context, null);
    }

    public ProgressCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        /**
         * 获得所有自定义的参数的值
         */
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.ProgressCircleView, defStyleAttr, 0);
        int n = a.getIndexCount();

        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.ProgressCircleView_defaultColor) {
                defaultColor = a.getColor(attr, defaultColor);

            } else if (attr == R.styleable.ProgressCircleView_progressColor) {
                progressColor = a.getColor(attr, progressColor);

            } else if (attr == R.styleable.ProgressCircleView_textColor) {
                textColor = a.getColor(attr, textColor);

            } else if (attr == R.styleable.ProgressCircleView_textSize) {
                textSize = a.getDimension(attr, textSize);

            } else if (attr == R.styleable.ProgressCircleView_strokeWidthPc) {
                strokeWidthPc = (int) a.getDimension(attr, strokeWidthPc);

            } else if (attr == R.styleable.ProgressCircleView_percent) {
                percent = a.getFloat(attr, percent);

            } else if (attr == R.styleable.ProgressCircleView_paddingPc) {
                paddingPc = (int) a.getDimension(attr, paddingPc);
            }
        }
        a.recycle();

        init(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        radius = getMeasuredWidth() / 2 - strokeWidthPc / 2 - paddingPc;

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int height;
        //高度测量
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = widthSize;
        }
        cx = getMeasuredWidth() / 2;
        cy = height / 2;

        rectF = new RectF(cx - radius, cy - radius, cx + radius, cy + radius);

        setMeasuredDimension(widthSize, height);
        //进度条的进度圆角处理
        /*this.rectF.left = getMeasuredWidth() / 2 - strokeWidth / 2;
        this.rectF.top = 0;
        this.rectF.right = getMeasuredWidth() / 2 + strokeWidth / 2;
        this.rectF.bottom = strokeWidth;*/
    }

    /**
     * 初始化一些参数
     */
    private void init(Context context) {
        mCirclePaint = new Paint();
        mCirclePaint.setColor(defaultColor);
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(strokeWidthPc);

        mTextPaint = new Paint();
        mTextPaint.setColor(textColor);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(textSize);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float drawPercent = percent;

        //先画一个深蓝色圆
        canvas.save();
        mCirclePaint.setColor(defaultColor);
        canvas.drawCircle(cx, cy, radius, mCirclePaint);
        canvas.restore();

        canvas.save();
        //选中进度中颜色
        mCirclePaint.setColor(progressColor);

        int endPercent = (int) (360 * drawPercent);
        canvas.drawArc(rectF, -90, endPercent, false, mCirclePaint);
        canvas.restore();

        int progressText = (int) (maxData * drawPercent);
        //进度文字
        canvas.drawText(progressText + "%", cx, cy + mTextPaint.measureText("00") / 2, mTextPaint);


    }

    /**
     * 更新进度
     *
     * @param currentData
     */
    public void setData(int currentData) {
        setData(currentData, 100);
    }

    public void setData(int currentData, int maxData) {
        if (maxData != 0)
            this.maxData = maxData;

        if (currentData > maxData) {
            currentData = maxData;
        }
        //目标所占的比例值
        float percentTemp = new BigDecimal(currentData).divide(new BigDecimal(this.maxData), 2, BigDecimal.ROUND_HALF_UP).floatValue();

        if (percentTemp < 0 || percentTemp > 1)
            return;
        this.percent = percentTemp;
        this.currentData = currentData;

        invalidate();
    }

    public void setDataWithAnimation(int currentData) {
        setDataWithAnimation(currentData, 100);
    }

    /**
     * 设置进度数据 带动画
     *
     * @param currentData 当前值 (currentData - 1)
     * @param maxData     最大值 (maxData -1 )
     */
    public void setDataWithAnimation(int currentData, int maxData) {
        if (maxData != 0)
            this.maxData = maxData;

        //上一次所占的比例值
        float oldPercent;
        if (this.currentData == 0)
            oldPercent = 0;
        else
            oldPercent = new BigDecimal(this.currentData).divide(new BigDecimal(this.maxData), 2, BigDecimal.ROUND_HALF_UP).floatValue();
        //目标所占的比例值
        float percentTemp = new BigDecimal(currentData).divide(new BigDecimal(this.maxData), 2, BigDecimal.ROUND_HALF_UP).floatValue();

        if (percentTemp < 0 || percentTemp > 1)
            return;
        this.percent = percentTemp;
        this.currentData = currentData;

        ObjectAnimator o = ObjectAnimator.ofFloat(this, "percent", oldPercent, percent);
        o.setDuration(1500);
        o.setInterpolator(new DecelerateInterpolator());
        o.start();
    }

    /**
     * 动画必须
     *
     * @param percent
     */
    public void setPercent(float percent) {
        this.percent = percent;
        invalidate();
    }


}
